//
//  LSZEnterInvitationCodeViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/6/5.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZEnterInvitationCodeViewController.h"

@interface LSZEnterInvitationCodeViewController ()<UINavigationControllerDelegate>
@property(nonatomic,strong) InsetsTextField * textField;
@end

@implementation LSZEnterInvitationCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;
    [self EnterInvitationCodeUI];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
-(void)EnterInvitationCodeUI
{
    UIImageView * imageViewBack = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    imageViewBack.image = [UIImage imageNamed:@"我的_输入邀请码背景图"];
    imageViewBack.userInteractionEnabled = YES;
    [self.view addSubview:imageViewBack];


    UIButton * butReturn = [[UIButton alloc]init];
    [butReturn setImage:[UIImage imageNamed:@"home_nav_button_back"] forState:UIControlStateNormal];
    [butReturn addTarget:self action:@selector(clickButReturn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:butReturn];
//    butReturn.backgroundColor = [UIColor yellowColor];

    UILabel * labelTitle = [[UILabel alloc]init];
    labelTitle.textColor = [UIColor whiteColor];
    labelTitle.text = @"输入邀请码";
    labelTitle.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:labelTitle];


    self.textField = [[InsetsTextField alloc]init];
    [self.textField.layer setMasksToBounds:YES];
    self.textField.backgroundColor = [UIColor whiteColor];
    self.textField.placeholder = @"请输入邀请码";
    [imageViewBack addSubview:self.textField];

    UILabel * labelNote = [[UILabel alloc]init];
    labelNote.textColor = RGBColor(249,233,156);
    labelNote.text = @"输入弟兄姊妹的邀请码您可以领取福音值";
    [imageViewBack addSubview:labelNote];

    UIButton * butOk = [[UIButton alloc]init];
    [butOk addTarget:self action:@selector(clickReceive) forControlEvents:UIControlEventTouchUpInside];
//    butOk.backgroundColor = [UIColor yellowColor];
    [butOk setImage:[UIImage imageNamed:@"确认领取"] forState:UIControlStateNormal];
    [imageViewBack addSubview:butOk];

    UILabel * label = [[UILabel alloc]init];
    label.text = @"向您的好友/推荐人询问邀请码";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = RGBColor(249,233,156);
    [imageViewBack addSubview:label];

    if (ScreenWidth == 320) {
        //5
        labelNote.font = FONT_(10);
        label.font = FONT_(13.5);
        labelTitle.font = FONT_(18);
        butReturn.frame = CGRectMake(0, STATUS_BAR_HEIGHT, 50, NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT);
        labelTitle.frame = CGRectMake(CGRectGetMaxX(butReturn.frame), STATUS_BAR_HEIGHT, ScreenWidth-CGRectGetMaxX(butReturn.frame)*2, NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT);
        self.textField.frame = CGRectMake(40, 50+NAVIGATION_BAR_HEIGHT, ScreenWidth-80, 38);
        [self.textField.layer setCornerRadius:19];
        labelNote.frame = CGRectMake(CGRectGetMinX(self.textField.frame)+15, CGRectGetMaxY(self.textField.frame)+5, self.textField.frame.size.width-15, 14);
        butOk.frame = CGRectMake((ScreenWidth-112)/2,ScreenHeight/119*65-56-5,112,112);

        label.frame = CGRectMake(0, CGRectGetMaxY(butOk.frame)+15, ScreenWidth, 25);
    }else if(ScreenWidth == 375){
        // 6 7
        labelNote.font = FONT_(12);
        label.font = FONT_(14);
        labelTitle.font = FONT_(18);
        butReturn.frame = CGRectMake(0, STATUS_BAR_HEIGHT, 50, NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT);
        labelTitle.frame = CGRectMake(CGRectGetMaxX(butReturn.frame), STATUS_BAR_HEIGHT, ScreenWidth-CGRectGetMaxX(butReturn.frame)*2, NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT);

        self.textField.frame = CGRectMake(55, 70+NAVIGATION_BAR_HEIGHT, ScreenWidth-110, 41);
        [self.textField.layer setCornerRadius:20.5];
        labelNote.frame = CGRectMake(CGRectGetMinX(self.textField.frame)+20, CGRectGetMaxY(self.textField.frame)+5, self.textField.frame.size.width-20, 16);
        butOk.frame = CGRectMake((ScreenWidth-120)/2,ScreenHeight/119*65-60-5,120,120);
        label.frame = CGRectMake(0, CGRectGetMaxY(butOk.frame)+15, ScreenWidth, 27);

    }else {
        //plus
        labelNote.font = FONT_(12.5);
        label.font = FONT_(14.5);
        labelTitle.font = FONT_(18);

        butReturn.frame = CGRectMake(0, STATUS_BAR_HEIGHT, 50, NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT);
        labelTitle.frame = CGRectMake(CGRectGetMaxX(butReturn.frame), STATUS_BAR_HEIGHT, ScreenWidth-CGRectGetMaxX(butReturn.frame)*2, NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT);

        self.textField.frame = CGRectMake(55, 90+NAVIGATION_BAR_HEIGHT, ScreenWidth-110, 43);
        [self.textField.layer setCornerRadius:21.5];
        labelNote.frame = CGRectMake(CGRectGetMinX(self.textField.frame)+20, CGRectGetMaxY(self.textField.frame)+5, self.textField.frame.size.width-20, 18);
        butOk.frame = CGRectMake((ScreenWidth-120)/2,ScreenHeight/119*65-62.5-5,125,125);
        label.frame = CGRectMake(0, CGRectGetMaxY(butOk.frame)+15, ScreenWidth, 29);

    }
}
#pragma mark - 领取
-(void)clickReceive
{
    if (self.textField.text.length >0) {
        [LSZRequestData enterTheInvitationCode:self.textField.text :^(BOOL isOk, NSString * str, NSDictionary * dic) {
            if(isOk == YES){
                [self setStringWarning:str];
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [self setStringWarning:str];
            }
        }];
    }else{
        [self setStringWarning:@"请输入邀请码"];
    }
}
-(void)clickButReturn
{
    [self.navigationController popViewControllerAnimated:YES];
}
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
@end
